package com.szeldon.pj.jps.visitor;

import java.util.logging.Logger;

import com.szeldon.pj.jps.data.store.SBAStore;
import com.szeldon.pj.jps.datastore.SimpleObject;
import com.szeldon.pj.jps.envs.ENVS;
import com.szeldon.pj.jps.exp.Expression;
import com.szeldon.pj.jps.exp.NaryExpression;
import com.szeldon.pj.jps.qres.AbstractQueryResult;
import com.szeldon.pj.jps.qres.IntegerResult;
import com.szeldon.pj.jps.qres.QResStack;
import com.szeldon.pj.jps.qres.RealResult;

public class MinNaryExpression extends NaryExpression {

	private static final Logger logger = Logger.getLogger(MinNaryExpression.class.getName());

	/**
	 * 
	 * @param expressions
	 */
	public MinNaryExpression(Expression... expressions) {
		super(expressions);
	}

	@Override
	public int compareTo(SimpleObject object) throws IllegalStateException {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public String getValueAsString() {
		// TODO Auto-generated method stub
		return null;
	}

	public void accept(ASTVisitor astVisitor, ENVS envs, QResStack qResStack, SBAStore store) {

		AbstractQueryResult minResult = null;
		double minValue = Double.MAX_VALUE;
		for (Expression currentExpression : expressions) {
			astVisitor.accept(currentExpression);
			AbstractQueryResult currentResult = qResStack.pop();

			if (currentResult instanceof IntegerResult) {
				int currentValue = ((IntegerResult) currentResult).getInteger();
				if (currentValue < minValue) {
					minResult = currentResult;
					minValue = currentValue;
				}
			} else if (currentResult instanceof RealResult) {
				double currentValue = ((RealResult) currentResult).getReal();
				if (currentValue < minValue) {
					minResult = currentResult;
					minValue = currentValue;
				}
			} else {
				throw new IllegalStateException("Min expression can't have an argument of "
						+ currentResult.getClass().getName() + " type.");
			}
		}

		qResStack.push(minResult);

	}
}
